package com.gxa.jjty.cjr.service.impl;

import com.gxa.jjty.cjr.mapper.EvaluateImgMapper;
import com.gxa.jjty.cjr.mapper.EvaluateMapper;
import com.gxa.jjty.cjr.mapper.MyEvaluateVoMapper;
import com.gxa.jjty.cjr.service.EvaluateService;
import com.gxa.jjty.core.pojo.Evaluate;
import com.gxa.jjty.core.pojo.EvaluateImg;
import com.gxa.jjty.core.pojo.MyEvaluateVo;
import com.gxa.jjty.core.service.impl.BaseServiceImpl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.ArrayList;
import java.util.List;

/**
 * @ClassName EvaluateServiceImpl
 * @Author: ws
 * @date 2021/12/8 10:02
 */
@Service("evaluateService")
public class EvaluateServiceImpl extends BaseServiceImpl<Evaluate> implements EvaluateService {

    @Autowired
    EvaluateMapper evaluateMapper;

    @Autowired
    EvaluateImgMapper evaluateImgMapper;

    @Autowired
    MyEvaluateVoMapper myEvaluateVoMapper;

    @Override
    public Integer insertImg(Long orderId, String imgUrls) {

        String[] urls = null;
        EvaluateImg evaluateImg = null;
        int updates = 0;
        if (imgUrls.length() > 0) {
//            urls = imgUrls.substring(1, imgUrls.length() - 1).split(",");
            urls = imgUrls.split(",");
            for (int i = 0; i < urls.length; i++) {

                evaluateImg = new EvaluateImg(orderId,urls[i]);

                System.out.println(evaluateImg);
                updates += evaluateImgMapper.insertSelective(evaluateImg);
            }
        }

        return updates;
    }

    @Override
    public List<MyEvaluateVo> getMyEvaluate(Long cjrId) {

        // 仅返回当前陪护员待评价的和自评的，拼接条件
        List<Integer> flagValues = new ArrayList<>(2);
        flagValues.add(1);
        flagValues.add(2);

        Example example = new Example(MyEvaluateVo.class);
        Example.Criteria criteria = example.createCriteria();

        criteria.andEqualTo("cjrId",cjrId);
        criteria.andIn("flag",flagValues);


        List<MyEvaluateVo> myEvaluateList = myEvaluateVoMapper.selectByExample(example);

        if (myEvaluateList.size() > 0) {
            EvaluateImg evaluateImg = new EvaluateImg();
            for (MyEvaluateVo myEvaluate : myEvaluateList) {
                evaluateImg.setOrderId(myEvaluate.getOrderId());

                List<EvaluateImg> select = evaluateImgMapper.select(evaluateImg);
                myEvaluate.setImgUrls(select);
            }
        }

        return myEvaluateList;
    }

    @Override
    public void deleteByOrderId(Long orderId) {

        Evaluate evaluate = new Evaluate();
        evaluate.setOrderId(orderId);
        evaluateMapper.delete(evaluate);

        EvaluateImg evaluateImg = new EvaluateImg();
        evaluateImg.setOrderId(orderId);
        evaluateImgMapper.delete(evaluateImg);
    }
}
